package dao;

import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * A data access object (DAO) providing persistence and search support for
 * GameTab entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see dao.GameTab
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class GameTabDAO {
	private static final Logger log = LoggerFactory.getLogger(GameTabDAO.class);
	// property constants
	public static final String GAME_NAME = "gameName";
	public static final String DESC = "desc";
	public static final String ICON = "icon";
	public static final String INDEX = "index";
	public static final String PIC_BIG = "picBig";
	public static final String PARTY_TITLE = "partyTitle";
	public static final String VERSION = "version";
	public static final String PIC_DIR = "picDir";
	public static final String BBS_INDEX = "bbsIndex";
	public static final String DOWNLOAD_URL = "downloadUrl";

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(GameTab transientInstance) {
		log.debug("saving GameTab instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(GameTab persistentInstance) {
		log.debug("deleting GameTab instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public GameTab findById(java.lang.Integer id) {
		log.debug("getting GameTab instance with id: " + id);
		try {
			GameTab instance = (GameTab) getCurrentSession().get("dao.GameTab",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<GameTab> findByExample(GameTab instance) {
		log.debug("finding GameTab instance by example");
		try {
			List<GameTab> results = (List<GameTab>) getCurrentSession()
					.createCriteria("dao.GameTab").add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GameTab instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GameTab as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<GameTab> findByGameName(Object gameName) {
		return findByProperty(GAME_NAME, gameName);
	}

	public List<GameTab> findByDesc(Object desc) {
		return findByProperty(DESC, desc);
	}

	public List<GameTab> findByIcon(Object icon) {
		return findByProperty(ICON, icon);
	}

	public List<GameTab> findByIndex(Object index) {
		return findByProperty(INDEX, index);
	}

	public List<GameTab> findByPicBig(Object picBig) {
		return findByProperty(PIC_BIG, picBig);
	}

	public List<GameTab> findByPartyTitle(Object partyTitle) {
		return findByProperty(PARTY_TITLE, partyTitle);
	}

	public List<GameTab> findByVersion(Object version) {
		return findByProperty(VERSION, version);
	}

	public List<GameTab> findByPicDir(Object picDir) {
		return findByProperty(PIC_DIR, picDir);
	}

	public List<GameTab> findByBbsIndex(Object bbsIndex) {
		return findByProperty(BBS_INDEX, bbsIndex);
	}

	public List<GameTab> findByDownloadUrl(Object downloadUrl) {
		return findByProperty(DOWNLOAD_URL, downloadUrl);
	}

	public List findAll() {
		log.debug("finding all GameTab instances");
		try {
			String queryString = "from GameTab";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public GameTab merge(GameTab detachedInstance) {
		log.debug("merging GameTab instance");
		try {
			GameTab result = (GameTab) getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GameTab instance) {
		log.debug("attaching dirty GameTab instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GameTab instance) {
		log.debug("attaching clean GameTab instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GameTabDAO getFromApplicationContext(ApplicationContext ctx) {
		return (GameTabDAO) ctx.getBean("GameTabDAO");
	}
}